/******************************************************************************************************
Copyright (C) 2011 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/
function nmrAddTest()
{
	var dw=mainWindow.activeDocument
	var numberOfNMR = dw.itemCount("NMR Spectrum")
	if(numberOfNMR>1) {
		var firstNMR = new NMRSpectrum(dw.item(0,"NMR Spectrum"))
		var secondNMR = new NMRSpectrum(dw.item(1,"NMR Spectrum"))
		if(firstNMR!=undefined && secondNMR!=undefined && firstNMR.isValid() && secondNMR.isValid()) {
			var newPage = dw.newPage()
			var resultNMR = new NMRSpectrum(newPage.cloneItems(firstNMR))
			var nmrArith = new NMRArithmetic
			if(nmrArith.add(firstNMR, secondNMR, resultNMR) == false) {
				MessageBox.critical(nmrArith.lastError, MessageBox.Ok)
				return false
			}
			resultNMR.fitToHeight()
			dw.setCurPage(newPage)
			newPage.update()
		}
	}
}

function nmrAddArrayedTest()
{
	var dw=mainWindow.activeDocument
	var numberOfNMR = dw.itemCount("NMR Spectrum")
	var stackedNMR = undefined
	var spcCount
	for(var i=0; i<numberOfNMR; ++i) {
		var nmrSpc = new NMRSpectrum(dw.item(i,"NMR Spectrum"))
		if(nmrSpc.specCount>1) {
			spcCount = nmrSpc.specCount
			stackedNMR = nmrSpc
			break
		}
	}
	if(stackedNMR == undefined) {
		MessageBox.critical("No Stacked Spectra", MessageBox.Ok)
		return false
	}

	var resultNMR = nmr.extractSpectrumFromArrayedItem(stackedNMR)
	if(resultNMR.isValid()) {
		var nmrArith = new NMRArithmetic
		if(nmrArith.add(stackedNMR, 0, stackedNMR, 1, resultNMR) == false) {
			MessageBox.critical(nmrArith.lastError, MessageBox.Ok)
			return false
		}
		resultNMR.fitToHeight()
	}
}

function nmrMulTest()
{
	var dw=mainWindow.activeDocument
	var numberOfNMR = dw.itemCount("NMR Spectrum")
	if(numberOfNMR>1) {
		var firstNMR = new NMRSpectrum(dw.item(0,"NMR Spectrum"))
		var secondNMR = new NMRSpectrum(dw.item(1,"NMR Spectrum"))
		if(firstNMR!=undefined && secondNMR!=undefined && firstNMR.isValid() && secondNMR.isValid()) {
			var newPage = dw.newPage()
			var resultNMR = new NMRSpectrum(newPage.cloneItems(firstNMR))
			var nmrArith = new NMRArithmetic
			if(nmrArith.mul(firstNMR, secondNMR, resultNMR) == false) {
				MessageBox.critical(nmrArith.lastError, MessageBox.Ok)
				return false
			}
			resultNMR.fitToHeight()
			dw.setCurPage(newPage)
			newPage.update()
		}
	}
}

function nmrScaleTest()
{
	var dw=mainWindow.activeDocument
	var numberOfNMR = dw.itemCount("NMR Spectrum")
	if(numberOfNMR) {
		var nmrSpc = new NMRSpectrum(dw.item(0,"NMR Spectrum"))
		if(nmrSpc!=undefined && nmrSpc.isValid()) {
			var newPage = dw.newPage()
			var resultNMR = new NMRSpectrum(newPage.cloneItems(nmrSpc))
			var nmrArith = new NMRArithmetic
			if(nmrArith.scale(0.5, nmrSpc, resultNMR) == false) {
				MessageBox.critical(nmrArith.lastError, MessageBox.Ok)
				return false
			}
			resultNMR.fitToHeight()
			dw.setCurPage(newPage)
			newPage.update()
		}
	}
}

function nmrScaleArrayedTest()
{
	var dw=mainWindow.activeDocument
	var numberOfNMR = dw.itemCount("NMR Spectrum")
	var stackedNMR = undefined
	var spcCount
	for(var i=0; i<numberOfNMR; ++i) {
		var nmrSpc = new NMRSpectrum(dw.item(i,"NMR Spectrum"))
		if(nmrSpc.specCount>1) {
			spcCount = nmrSpc.specCount
			stackedNMR = nmrSpc
			break
		}
	}
	if(stackedNMR == undefined) {
		MessageBox.critical("No Stacked Spectra", MessageBox.Ok)
		return false
	}

	var resultNMR = nmr.extractSpectrumFromArrayedItem(stackedNMR)
	if(resultNMR.isValid()) {
		var nmrArith = new NMRArithmetic
		if(nmrArith.scale(0.5,stackedNMR, 0, resultNMR) == false) {
			MessageBox.critical(nmrArith.lastError, MessageBox.Ok)
			return false
		}
		resultNMR.fitToHeight()
	}
}
